package com.company.project.web;

import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.SysUser;
import com.company.project.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @author
 * @description:
 * @date 2020/3/24
 */
@RestController
public class HomeController {

    @Autowired
    SysUserService userService;

    @RequestMapping("/")
    public ModelAndView home(){
        ModelAndView modelAndView = new ModelAndView("/login/login.html");
        return modelAndView;
    }

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("/workspace/index.html");
        return modelAndView;
    }

    @RequestMapping("/main")
    public ModelAndView main(){
        ModelAndView modelAndView = new ModelAndView("/workspace/main.html");
        return modelAndView;
    }



    @RequestMapping("/login")
    public Result login(String userName, String pwd, HttpSession session) {
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(userName,pwd);
        Subject subject = SecurityUtils.getSubject();
        try {
            //完成登录
            subject.login(usernamePasswordToken);
            SysUser user=(SysUser) subject.getPrincipal();
            session.setAttribute("user", user);
            return ResultGenerator.genSuccessResult(user);
        } catch(Exception e) {
            //返回登录页面
            return ResultGenerator.genFailResult();
        }

    }

    @RequestMapping("/toRegister")
    public ModelAndView toRegister(){
        ModelAndView modelAndView = new ModelAndView("/login/register.html");
        return modelAndView;
    }

    @RequestMapping("/toCompanyRegister")
    public ModelAndView toCompanyRegister(){
        ModelAndView modelAndView = new ModelAndView("/login/company_register.html");
        return modelAndView;
    }
    @RequestMapping("/toTeacherRegister")
    public ModelAndView toTeacherRegister(){
        ModelAndView modelAndView = new ModelAndView("/login/teacher_register.html");
        return modelAndView;
    }

    /**
     * 注册用户
     * @param userName 用户名
     * @param phone 手机号
     * @param email 邮箱地址
     * @param userType 用户类型 user:学生;teacher:实习管理人员;company:实习企业
     * @param idCard
     * @return
     */
    @RequestMapping("/register")
    public Result register(@RequestParam("userName") String userName, @RequestParam("phone") String phone,
                                   @RequestParam("email") String email, @RequestParam("pwd") String pwd
            , @RequestParam("userType") String userType,String idCard){
        return userService.register(userName,phone,email,pwd,userType,idCard);
    }


    @RequestMapping("/403")
    public String unauthorizedRole(){
        System.out.println("------没有权限-------");
        return "403";
    }

}
